package cn.iocoder.yudao.module.dine.config.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.annotation.AdminJsonView;
import cn.iocoder.yudao.framework.business.basic.controller.CrudController;
import cn.iocoder.yudao.framework.business.basic.controller.ListController;
import cn.iocoder.yudao.framework.business.basic.pojo.dto.SimpleQueryDTO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.dine.vo.DineTimeRangeVO;
import cn.iocoder.yudao.module.api.product.category.ProductCategoryApi;
import cn.iocoder.yudao.module.api.product.category.dto.CategoryRespDTO;
import cn.iocoder.yudao.module.dine.config.pojo.dto.DineTimeRangeSaveDTO;
import cn.iocoder.yudao.module.dine.config.service.admin.DineTimeRangeService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 前端控制器
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/09/07
 */
@Tag(name = "管理后台 - 点餐时段配置")
@RestController
@RequestMapping("/dine/config/time-range")
public class DineTimeRangeController implements CrudController<DineTimeRangeVO, DineTimeRangeSaveDTO, SimpleQueryDTO>, ListController<DineTimeRangeVO, SimpleQueryDTO> {
    @Resource
    private DineTimeRangeService dineTimeRangeService;

    @Resource
    private ProductCategoryApi productCategoryApi;

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin','shop_admin')")
    @JsonView(AdminJsonView.class)
    @Override
    public CommonResult<DineTimeRangeVO> getDetail(Long id) {
        return CommonResult.success(dineTimeRangeService.getDetail(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin','shop_admin')")
    @JsonView(AdminJsonView.class)
    @Override
    public CommonResult<PageResult<DineTimeRangeVO>> getPage(Integer pageNo, Integer pageSize, SimpleQueryDTO param) {
        PageResult<DineTimeRangeVO> page = dineTimeRangeService.getPage(pageNo, pageSize, param);
        if (CollUtil.isEmpty(page.getList())) {
            return CommonResult.success(PageResult.empty());
        }
        return CommonResult.success(new PageResult<>(toCommonResult(page.getList()), page.getTotal()));
    }

    @PreAuthorize("@ss.hasRole('shop_admin')")
    @Override
    public CommonResult<List<DineTimeRangeVO>> getList(SimpleQueryDTO param) {
        List<DineTimeRangeVO> list = dineTimeRangeService.getList(param);
        return CommonResult.success(toCommonResult(list));
    }

    private List<DineTimeRangeVO> toCommonResult(List<DineTimeRangeVO> list) {
        if (CollUtil.isEmpty(list)) {
            return list;
        }
        Set<Long> categoryIds = list.stream().map(DineTimeRangeVO::getCategoryId).collect(Collectors.toSet());
        List<CategoryRespDTO> categories = productCategoryApi.getCategoryList(categoryIds);
        if (CollUtil.isEmpty(categories)) {
            return list;
        }
        Map<Long, CategoryRespDTO> categoryMap = categories.stream().collect(Collectors.toMap(CategoryRespDTO::getId, it -> it));
        list.forEach(item -> item.setCategoryName(categoryMap.get(item.getCategoryId()).getName()));
        return list;
    }

    @PreAuthorize("@ss.hasRole('shop_admin')")
    @Override
    public CommonResult<Long> create(DineTimeRangeSaveDTO param) {
        return CommonResult.success(dineTimeRangeService.create(param));
    }

    @PreAuthorize("@ss.hasRole('shop_admin')")
    @Override
    public CommonResult<Boolean> delete(Long id) {
        return CommonResult.success(dineTimeRangeService.delete(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin','shop_admin')")
    @Override
    public CommonResult<Boolean> update(DineTimeRangeSaveDTO param) {
        return CommonResult.success(dineTimeRangeService.update(param));
    }

}
